/**
 * service_safety js file;
 * company:junqitecch
 * author:xuzuiotao
 * 
 */

//test
//
// LOGOUT:32,
//        USER_DETAIL:33,
//        USER_CHANGE_PASSWORD:34,


/**
 * login js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var mail = require('../network/email');
var users= require('../db/users');
var cache=require('../redis/usercache');
var bind= require('../db/bind');
var sms = require('../network/httpsms');
var checkSMS = require('../db/mphonemsg');
var im= require('../network/im');
var qiniu=require('../network/qiniu');
var crypto=require('../utils/crypto');

function processBind(request,response,callback){
	console.log('processBind');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.extra)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2&& param.type!==3){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		bind.bindAccount(entity.userID, param.type, param.extra, function(entity){
			if(entity===null){
				response.errorCode=consts.Error.ERROR_BIND_ERROR;
				response.message=consts.Message.ERROR_BIND_ERROR;
				if(callback){
					callback(response);
				}
			}else{
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data = JSON.stringify(entity);
				response.token=tk;

				if(callback){
					callback(response);
				}
			}
		});
	});

}


function processChangeMobile(request,response,callback){
	console.log('processChangeMobile');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	

	var changevals = [];
	var i =0;
	if(param.mobile!==null){
		changevals.phone_number=param.mobile;
		i++;
	}
	
	if(i<=0){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	

	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		users.update(entity.userID,changevals,function(err,entity){
			if(err){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				entity.qiniuToken=qiniu.uptoken(null);

				im.getIMToken(entity,function(err){
					cache.refresh(entity,function(token,user){
	    				if(token){
	    					response.errorCode = consts.Error.NONE_ERROR;
	    					response.message= consts.Message.OK_MESSAGE;
	    					response.data = JSON.stringify(entity);
	    					response.token=token;

	    					if(callback){
	    						callback(response);
	    					}
	    				}else{
	    					response.errorCode = consts.Error.ERROR_CHECK;
	    					response.message= consts.Message.ERROR_LOGIN;
	    					if(callback){
	    						callback(response);
	    					}
	    				}
	    				
	    			});
	    			
				});
				
			}
		});
	});


}

function processChangeMobileSMS(request,response,callback){
	console.log('processChangeMobileSMS');
	var data =request.data;
	console.log('111111');

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		console.log('22222');
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	if(param.mobile===null || typeof(param.mobile)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':mobile';
		console.log('5555555');
		if(callback){
			callback(response);
		}
		return false;
	}
	
	console.log('666666');
	var canSend = false;

	users.getEntityByMobileNOPass(param.mobile,function(entity){
	    
	
		if(entity!==null){
			response.errorCode=consts.Error.ERROR_MOBILE_USED;
			response.message=consts.Message.ERROR_MOBILE_USED;
			
		}else{
			canSend=true;
		}
		
		if(canSend){
			sms.sendVeriCode(param.mobile, response, function(){
				if(callback){
					callback(response);
				}

			});
			
		}else{
			if(callback){
				callback(response);
			}
		}
		
	});
	
}


function processPaySignList(request, response,callback){

	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	users.getPaySignList(request.token,function(wrap){
		if(wrap===null){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
		}else{
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			response.token=request.token;
			if(callback){
				callback(response);
			
			}
			
		}
		
	});

	return;
	
}


function processChangePassword(request, response,callback){
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	}
    

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	if(param.newPassword===null || typeof(param.newPassword)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':newPassword';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	param.newPassword= crypto.des.encrypt(param.newPassword);

	
	
	if(param.password===null || typeof(param.password)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':password';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	param.password= crypto.des.encrypt(param.password);

	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}

		users.changePassword(entity.userID,param.password,param.newPassword,function(err,entity){
			if(err){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}

			}else{
				if(entity){
					entity.qiniuToken=qiniu.uptoken(null);

					im.getIMToken(entity,function(err){
						cache.cache(entity,function(token,user){
		    				if(token){
		    					response.errorCode = consts.Error.NONE_ERROR;
		    					response.message= consts.Message.OK_MESSAGE;
		    					response.data = JSON.stringify(entity);
		    					response.token=token;

		    					if(callback){
		    						callback(response);
		    					}
		    				}else{
		    					response.errorCode = consts.Error.ERROR_CHECK;
		    					response.message= consts.Message.ERROR_LOGIN;
		    					if(callback){
		    						callback(response);
		    					}
		    				}
		    				
		    			});
		    			
					});
					
				}else{
					
					response.errorCode = consts.Error.ERROR_CHECK;
					response.message= consts.Message.ERROR_LOGIN;
					if(callback){
						callback(response);
					}
					response.token=tk;
					if(callback){
						callback(response);
					}
					
				}
					
			}
		});
	});

}

module.exports = {
		processChangeMobile:processChangeMobile,
		processBind:processBind,
		processPaySignList:processPaySignList,
		processChangePassword:processChangePassword,
		processChangeMobileSMS:processChangeMobileSMS,

	  };
